package com.hg.avaya.util;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.avaya.sce.runtime.Prompt;
import com.avaya.sce.runtime.PromptElement;
import com.avaya.sce.runtimecommon.SCESession;

public class PromptUtil {

  /**
   * 话术的名字
   */
  private Prompt prompt;
  /*
   * 日志名
   */
  private static Logger logger = Logger.getLogger(PromptUtil.class);

  public PromptUtil(Prompt prompt) {
    this.prompt = prompt;
  }

  /**
   * 更新话术
   * @param mySession
   */
  public void updatePrompt(SCESession mySession) {
    // true:支持打断,false,不支持打断
    boolean bargeIn = SCESessionUtil.getBargIn(mySession);
    prompt.setBargin(1, bargeIn);

    List<String[]> list = SCESessionUtil.getPalyBeans(mySession);
    /**
     * 遍历list,根据不同的类型设置不同的播放方式
     */
    for (String[] item : list) {
      if (item[1].toLowerCase().equals("wav")) {
        PlayWav(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("digits")) {
        PlayDigits(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("number")) {
        PlayNumber(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("md_currency")) {
        PlayMDCurrency(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("us_currency")) {
        PlayUSCurrency(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("date")) {
        PlayDate(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("tts")) {
        PlayTTS(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("time")) {
        PlayTime(mySession, item[0]);
      } else if (item[1].toLowerCase().equals("month")) {
        String value = (String) GetVariableValue(mySession, item[0]);
        StringBuffer sb = new StringBuffer();
        String yearmonth = sb.append(value.substring(0, 4)).append("年").append(value.substring(4, 6))
            .append("月").toString();
        SetVariableValue(mySession, item[0], yearmonth);
        PlayTTS(mySession, item[0]);
      }
    }

  }

  /**
   * 播放wav类型声音
   * @param mySession
   * @param variableName
   */
  public void PlayWav(SCESession mySession, String variableName) {
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"wav\">" + variableName + "</play>");
    }
    prompt.add(1, new PromptElement(PromptElement.PHRASESET, variableName, false));
  }

  public void PlayDigits(SCESession mySession, String variableName) {
    String value = (String) GetVariableValue(mySession, variableName);
    try {
      // BigDecimal bigDecimal = new BigDecimal(value);
      // value = bigDecimal.toPlainString();
      SetVariableValue(mySession, variableName, value);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"digits\">" + value + "</play>");
    }
    // com.avaya.sce.runtime.Format format = new
    // com.avaya.sce.runtime.Format();
    // format.add(com.avaya.sce.runtime.Format.FORMAT,
    // com.avaya.sce.runtime.Format.FMT_DIGITS);
    // prompt.add(1, new com.avaya.sce.runtime.PromptElement(
    // com.avaya.sce.runtime.PromptElement.VARIABLE_TEXT,
    // variableName, format));

    com.avaya.sce.runtime.Format format = new com.avaya.sce.runtime.Format();
    format.add("format", "digits");
    format.add("inflection", "Crmm");
    prompt.add(1, new com.avaya.sce.runtime.PromptElement(
        com.avaya.sce.runtime.PromptElement.VARIABLE_AUDIO, variableName, format, false));
  }

  public void PlayNumber(SCESession mySession, String variableName) {
    String value = GetVariableValue(mySession, variableName).toString();
    try {
      BigDecimal bigDecimal = new BigDecimal(value);
      value = bigDecimal.toPlainString();
      SetVariableValue(mySession, variableName, value);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"number\">" + value + "</play>");
    }
    com.avaya.sce.runtime.Format format = new com.avaya.sce.runtime.Format();
    format.add("format", "number");
    if (value.matches("\\d+")) {
      format.add("numbertype", "Nn");
    } else if (value.matches("\\d+\\.\\d++")) {
      format.add("numbertype", "NDn");
    } else {
      return;
    }
    prompt.add(1, new com.avaya.sce.runtime.PromptElement(
        com.avaya.sce.runtime.PromptElement.VARIABLE_AUDIO, variableName, format, false));
  }

  public void PlayMDCurrency(SCESession mySession, String variableName) {
    String value = (String) GetVariableValue(mySession, variableName);
    try {
      if (value.startsWith("-")) {
        value = value.replace("-", "");
      }
      BigDecimal bigDecimal = new BigDecimal(value);
      value = bigDecimal.toPlainString();
      if (value != null) {
        if (value.matches("\\d+\\.0$")) {
          value = value.substring(0, value.length() - 2);
        } else if (value.matches("\\d+\\.[1-9]{1}0$")) {
          value = value.substring(0, value.length() - 1);
        } else if (value.matches("\\d+\\.00$")) {
          value = value.substring(0, value.length() - 3);
        } else if (value.matches("\\d+\\.$")) {
          value = value.substring(0, value.length() - 1);
        }
      }
      SetVariableValue(mySession, variableName, value);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"md_currency\">" + value + "</play>");
    }
    if (value.matches("\\d+\\.{0,1}\\d+") || value.matches("\\d+")) {

      PlayNumber(mySession, variableName);
      PlayWav(mySession, "Base:1028");
    }
  }

  public void PlayUSCurrency(SCESession mySession, String variableName) {
    String value = (String) GetVariableValue(mySession, variableName);
    try {
      if (value.startsWith("-")) {
        value = value.replace("-", "");
      }
      BigDecimal bigDecimal = new BigDecimal(value);
      value = bigDecimal.toPlainString();
      if (value != null) {
        if (value.matches("\\d+\\.0$")) {
          value = value.substring(0, value.length() - 2);
        } else if (value.matches("\\d+\\.[1-9]{1}0$")) {
          value = value.substring(0, value.length() - 1);
        } else if (value.matches("\\d+\\.00$")) {
          value = value.substring(0, value.length() - 3);
        } else if (value.matches("\\d+\\.$")) {
          value = value.substring(0, value.length() - 1);
        }
      }
      SetVariableValue(mySession, variableName, value);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"us_currency\">" + value + "</play>");
    }
    if (value.matches("\\d+\\.{0,1}\\d+") || value.matches("\\d+")) {

      PlayNumber(mySession, variableName);

      String language = mySession.getCurrentLanguage();
      if ("english".equals(language)) {
        PlayWav(mySession, "std_en_us:std_dollar");
      } else {
        PlayWav(mySession, "Base:1034");
      }
    }
  }

  public void PlayDate(SCESession mySession, String variableName) {
    String value = (String) GetVariableValue(mySession, variableName);
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"date\">" + value + "</play>");
    }
    if (value.matches("\\d{8}")) {
      com.avaya.sce.runtime.Format format = new com.avaya.sce.runtime.Format();
      format.add("format", "date");
      format.add("dateformat", "DYYMD");
      prompt.add(1, new com.avaya.sce.runtime.PromptElement(
          com.avaya.sce.runtime.PromptElement.VARIABLE_AUDIO, variableName, format, false));
    }
  }

  /**
   * 设置播放TTS话术
   * @param mySession
   * @param variableName
   */
  public void PlayTTS(SCESession mySession, String variableName) {
    String value = variableName;
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"tts\">" + value + "</play>");
    }
    prompt.add(1, new PromptElement(PromptElement.TEXT, value, null));
  }

  public void PlayTime(SCESession mySession, String variableName) {
    String value = (String) GetVariableValue(mySession, variableName);
    if (logger.isDebugEnabled()) {
      logger.debug(DataFormat.getMySessionId(mySession) + "<play language=\""
          + mySession.getCurrentLanguage() + "\" type=\"time\">" + value + "</play>");
    }
    if (value.matches("\\d+")) {
      com.avaya.sce.runtime.Format format = new com.avaya.sce.runtime.Format();
      format.add("format", "time");
      format.add("timetype", "THMAM");
      prompt.add(1, new com.avaya.sce.runtime.PromptElement(
          com.avaya.sce.runtime.PromptElement.VARIABLE_AUDIO, variableName, format, false));
    }
  }

  public Object GetVariableValue(SCESession mySession, String variableName) {
    Object object = null;
    if (variableName.indexOf(':') == -1) {
      object = mySession.getVariable(variableName).getSimpleVariable().getObjectValue();
    } else {
      String[] array = variableName.split(":");
      object = mySession.getVariableField(array[0], array[1]).getObjectValue();
    }
    return object;
  }

  public void SetVariableValue(SCESession mySession, String variableName, String value) {

    if (variableName.indexOf(':') == -1) {
      mySession.getVariableField(variableName).setValue(value);
    } else {
      String[] array = variableName.split(":");
      mySession.getVariableField(array[0], array[1]).setValue(value);
    }

  }
}